//
//  DrawViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "DrawViewController.h"

/// logical: 在给navagation初始化的时候就加上左右视图，并让navagation为跟控制器（rightVC为navagation的跟控制器），menuVC点击时调用代理方法换rightVC的图片，DrawView管理两个控制器

@interface DrawViewController ()<MenuViewControllerDelefate>
@property(nonatomic,strong)MenuViewController *menuVC;//接受菜单栏
@property(nonatomic,strong)UINavigationController *rightVC;//右侧试图导航栏
@property(nonatomic,strong)UIImageView *startImageView;
@end

@implementation DrawViewController

//开始时, 启动动画
- (void)test {
    self.startImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.startImageView.image = [UIImage imageNamed:@"9999.jpg"];
    [self.view addSubview:self.startImageView];
    
    [UIView animateWithDuration:10.0 animations:^{
        self.startImageView.transform = CGAffineTransformMakeScale(1.5, 1.5);
        self.startImageView.alpha = 0.0;
    } completion:^(BOOL finished) {

        
        //添加子视图
        [self.view addSubview:self.menuVC.view];
        [self.view addSubview:self.rightVC.view];
        
        [self.startImageView removeFromSuperview];
    }];
}

//自定义初始化方法，在初始化的时候就直接给上左右控制器
- (instancetype)initWithMenuViewController:(MenuViewController *)menuVC  rightViewController:(RightViewController *)rightVC
{
    self = [super init];
    if (self) {
        self.menuVC = menuVC;
        self.menuVC.delegate = self;
        self.rightVC = [[UINavigationController alloc] initWithRootViewController:rightVC];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加子视图控制器
    [self addChildViewController:self.menuVC];
    [self addChildViewController:self.rightVC];
    
    [self  test];
    
}

//实协议方法更换右视图
- (void)menuViewControllerDidClickCellWithVC:(RightViewController *)VC{
    
    //替换之前先把旧的向右偏移
    [UIView  animateWithDuration:0.2 animations:^{
        self.rightVC.view.frame = CGRectMake(kwidth, 0, kwidth, kheight);
    } completion:^(BOOL finished) {
        //先移除旧的
        [self.rightVC removeFromParentViewController];
        [self.rightVC.view  removeFromSuperview];
        
        //在添加新的
        self.rightVC = [[UINavigationController alloc] initWithRootViewController:VC];
        [self  addChildViewController:self.rightVC];
        [self.view  addSubview:self.rightVC.view];
        //注意：这个得写
        self.rightVC.view.frame = CGRectMake(kwidth, 0, kwidth, kheight);
        
        //把新的在偏移回来
        [UIView  animateWithDuration:0.2 delay:0.1 options:UIViewAnimationOptionLayoutSubviews animations:^{
            self.rightVC.view.frame = self.view.frame;
        } completion:^(BOOL finished) {

        }];
    }];
    
}


@end
